package b1.scenes.detect
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.home.*;
	import b1.scenes.suspect.*;
	import b1.utils.BFont;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.*;

	public class SpecialCaseItem extends Sprite
	{
		private var specialCaseConfig:SpecialCaseConfig;
		//案件信息onMouseOver
		private var animalDetectTip:Sprite;
		
		public function SpecialCaseItem(specialCaseConfig:SpecialCaseConfig)
		{
			super();
			this.specialCaseConfig = specialCaseConfig;
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var tabItemBackground:Sprite = new TabItemBackground(); 
			tabItemBackground.width = 125;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			//案件背景图片
			var CaseBgrImg:Class = ResourceHelper.getInstance().getClassByName(specialCaseConfig.backgroundImageInDetectDialog);
			var caseBgr:Sprite = new CaseBgrImg(); 
			addChild(caseBgr);
			tabItemBackground = new TabItemBackground();
			tabItemBackground.width = 125;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			caseBgr.width = tabItemBackground.width;
			caseBgr.height = tabItemBackground.height;
			caseBgr.alpha = 0.9;
			caseBgr.mask = tabItemBackground;
			addChild(caseBgr);
			//案件图标
			var CaseIconImg:Class = ResourceHelper.getInstance().getClassByName(specialCaseConfig.iconImageInDetectDialog);
			var caseIcon:Sprite = new CaseIconImg();
			caseIcon.height = 70;
			caseIcon.scaleX = caseIcon.scaleY;
			caseIcon.x = (this.width - caseIcon.width) / 2;
			caseIcon.y = 10;
			addChild(caseIcon);
			//案件名称
			var titleText:TextField = new TextField();
			titleText.selectable = false;
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			titleText.text = specialCaseConfig.name;
			titleText.x = (tabItemBackground.width - titleText.width) / 2;
			titleText.y = 75;
			addChild(titleText);
			
			//案件副标题
			var subTitleText:TextField = new TextField();
			subTitleText.selectable = false;
			subTitleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
        	subTitleText.autoSize = TextFieldAutoSize.LEFT;
			subTitleText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000);
			subTitleText.text = specialCaseConfig.subName;
			subTitleText.x = (tabItemBackground.width - subTitleText.width) / 2;
			subTitleText.y = titleText.y + 20;
			addChild(subTitleText);
			
			addEventListener(MouseEvent.CLICK, onSpecialCaseItemClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		//点击特殊案件
		public function onSpecialCaseItemClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			if (!(SceneContainer.getInstance().currentScene is Home))
				return;
			var home:Home = SceneContainer.getInstance().currentScene as Home;
			var me:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
			var myAnimal:AnimalInfo = PlayerInfoHelper.getInstance().animals[me.ownAnimals[0]];
			if (myAnimal.timeToGetFree > ServerHelper.getInstance().now())
			{
				var msgBox:MessageBox = new MessageBox("你还未刑满释放，没有资格探查特殊案件……");
				msgBox.x = (BFont.stageWidth - msgBox.width) / 2;
				msgBox.y = (BFont.stageHeight - msgBox.height) / 2;
				home.addChild(msgBox);
			}
			else if (home.liveId != PlayerInfoHelper.getInstance().userId)
			{
				msgBox = new MessageBox("这是特殊案件，回到自己家中后才能探查！");
				msgBox.x = (BFont.stageWidth - msgBox.width) / 2;
				msgBox.y = (BFont.stageHeight - msgBox.height) / 2;
				home.addChild(msgBox);
			}
			else
			{
				var helperMessageBox:MessageBoxWithIcon = new MessageBoxWithIcon("探查特殊案件将进入故事模式。是否继续？", "id:-5", Config.assistantName, 1, true);
				helperMessageBox.x = BFont.stageWidth / 2;
				helperMessageBox.y = BFont.stageHeight / 2;
				home.addChild(helperMessageBox);
				helperMessageBox.buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
				//添加新手导引
				if (SceneContainer.getInstance().step == 10)
					SceneContainer.getInstance().addStep();
			}
		}
		
		//单击进入探案界面
		private function onButtonOKClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			DetectDiaglog.getInstance().close();
			StoryHelper.getInstance().startStory(specialCaseConfig);
			//添加新手导引
			if (SceneContainer.getInstance().step == 11)
				SceneContainer.getInstance().clearLastStep();
		}
		
		public function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(
				[1,0,0,0,15,
			     0,1,0,0,15,
			     0,0,1,0,15,
			     0,0,0,1,0 ])];
			animalDetectTip = new Sprite();
			var roundRec:RoundRec = new RoundRec(130, 135, 0.8, BFont.roundRecColor);
			animalDetectTip.addChild(roundRec);
			
			//“单击探案”
			var helpText:TextField = new TextField();
			helpText.selectable = false;
			helpText.width = roundRec.width;
			helpText.autoSize = TextFieldAutoSize.CENTER;
			var helpTexFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			helpTexFormat.leftMargin = 10;
			helpTexFormat.rightMargin = 10;
			helpTexFormat.color = 0x008000;
			helpText.defaultTextFormat = helpTexFormat;
			helpText.text = "单击探案";
			helpText.y = 10;
			animalDetectTip.addChild(helpText);
			
			var gap:int = 10;
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			titleText.x = gap;
			titleText.text = specialCaseConfig.name;
			titleText.y = 30;
			animalDetectTip.addChild(titleText);
			//事件描述
			var caseText:TextField = new TextField();
			caseText.selectable = false;
        	caseText.autoSize = TextFieldAutoSize.LEFT;
			caseText.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000);
			caseText.width = roundRec.width - 20;
			caseText.wordWrap = true;
			caseText.appendText(specialCaseConfig.description);
			caseText.x = gap;
			caseText.y = 50;
			animalDetectTip.addChild(caseText);
			
			if (this.x < 350)
			{
				animalDetectTip.x = this.x + 280;
			}
			else
			{
				animalDetectTip.x = this.x + 27;
			}
			animalDetectTip.y = this.y + 105;
			SceneContainer.getInstance().currentScene.addChild(animalDetectTip);
		}
		
		public function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			if (null != animalDetectTip && null != animalDetectTip.parent)
			{
				animalDetectTip.parent.removeChild(animalDetectTip);
			}
		}
	}
}